package com.controller;
import com.github.pagehelper.PageInfo;
import com.pojo.Tag;
import com.service.ProjectService;
import com.service.TagService;
import com.util.R;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@RestController
@CrossOrigin
public class TagController {

    @Autowired
    private TagService tagService;
    @Autowired
    private ProjectService projectService;

    //查询所有广告
    @RequestMapping("/selectAllTags")
    public Object selectAllTags(@RequestParam(defaultValue = "") String authtatus, @RequestParam(defaultValue = "1") int pageNum) {
        PageInfo<Tag> tagPageInfo = tagService.selectAll(authtatus, pageNum);
        R r = new R<Tag>();
        r.setCode(200);
        r.setData(tagPageInfo);
        return r;
    }

    //预览广告图片
    @RequestMapping("/vueUpload")
    public void vueUpload(String filename, HttpServletResponse response) throws IOException {
        File file = new File("C:\\Users\\Administrator\\Desktop\\广告", filename);
        FileInputStream fis = new FileInputStream(file);
//      attachement;
        response.setHeader("Content-Disposition", "filename=" + filename);
        ServletOutputStream sos = response.getOutputStream();
        IOUtils.copy(fis, sos);
    }

    //审核广告状态
    @RequestMapping("/updateTagStatus")
    public Object updateTagStatus(@RequestParam(defaultValue = "") String authtatus, int id) {
        int i = tagService.updateStatus(id, authtatus);
        R r = new R<Tag>();
        if (i != 0) {
            r.setCode(200);
            r.setMessage("修该成功");
        } else if (i == 0) {
            r.setCode(400);
            r.setMessage("修该失败");
        }
        return r;
    }

    //通过id查询广告
    @RequestMapping("/selectByTagId")
    public Object selectByTagId(long id) {
        Tag tag = tagService.selectById(id);

        if (tag != null) {
            return new com.pojo.R("", "200", tag);
        } else {
            return new com.pojo.R<>("没有该广告", "400", null);
        }
    }

    //随机查询两条广告显示
    @RequestMapping("/selectTwoTags")
    public Object selectTwoTags(@RequestParam(defaultValue = "") String authtatus, @RequestParam(defaultValue = "1") int pageNum) {
        PageInfo<Tag> tagPageInfo = tagService.selectAll(authtatus, pageNum);

        R r = new R<Tag>();
        r.setCode(200);
        r.setData(tagPageInfo);
        return r;
    }

    //删除广告信息假删
    @RequestMapping("/deleteTag")
    public Object deleteTaglong(long id) {
        int i = tagService.deleteTag(id);
        R r = new R<Tag>();
        if (i != 0) {
            r.setCode(200);
            r.setMessage("删除成功");
        } else if (i == 0) {
            r.setCode(400);
            r.setMessage("删除失败");
        }
        return r;
    }

    //insertTag插入广告
    @RequestMapping("/insertTag")
    public Object insertTag(long projectId) {
        R r = new R<Tag>();
        Tag tag = tagService.selectByPId(projectId);
        if (tag != null) {
            r.setCode(500);
            r.setMessage("广告已存在，不可重复发布");
        } else {
            int i = tagService.insertTag(projectId);
            if (i != 0) {
                r.setCode(200);
                r.setMessage("插入成功");
                r.setData(1);
            } else {
                r.setCode(400);
                r.setMessage("插入失败");
                r.setData(0);
            }
        }
        return r;
    }
}
